/**
 * Class for work with vkontakte.ru. 
 */
function Vkontakte(){
  // initialization settings
  var APP_ID = 2242336;  // id of application
  var UPDATE_STATUS = 1; // VK.Auth.getLoginStatus right after initialization
  
  var LOGIN_REDIRECT_URL = "/process/vkontakte/";
  var LOGOUT_REDIRECT_URL = "/";


  /**
   * Initializes vkontakte API.
   */  
  this.initVkontakteAPI = function()
  {
    window.vkAsyncInit = function()
    {
      VK.init({apiId: APP_ID, vk: UPDATE_STATUS});
    };
  
    // parameters for vkontakte, change only if changed API
    var createScriptElement = function() {
      var el = document.createElement("script");
      el.type = "text/javascript";
      el.src = "http://vkontakte.ru/js/api/openapi.js";
      el.async = true;
      document.getElementById("vk_api_transport").appendChild(el);
    }
    
    setTimeout(createScriptElement, 0);
  }
 
  /**
   * Authorizes user in vkontakte.ru
   */
  this.doLogin = function()
  {
    VK.Auth.login(loginHandler);
  }
  
  /**
   * Processes login function request
   * 
   * @param {Object} response from vkontakte.ru
   */
  var loginHandler = function(response)
  {
    if (response.session)
    {
      window.location = LOGIN_REDIRECT_URL;
    }
    else
    {
      js.include('rare_tools');
      UItools.showAuthError();
    }
  }
  
  /**
   * Logout user in vkontakte.ru
   */
  this.doLogout = function()
  {
    VK.Auth.logout(logoutHandler);
  }
  
  /**
   * Processes logout function request
   * 
   * @param {Object} response from vkontakte.ru
   */
  var logoutHandler = function(response)
  {
    window.location = LOGOUT_REDIRECT_URL;
  }
}